package com.qf.boot2302.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.boot2302.entity.Student;
import com.qf.boot2302.mapper.StudentMapper;
import com.qf.boot2302.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Random;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;

    @Override
    public Student findPickOneStudent() {
        List<Student> list = studentMapper.findSatusIsZero();
        if (list.size()==0){
            return null;
        }
        int index = new Random().nextInt(list.size());
        Student student = list.get(index);
        studentMapper.updateStudentStatus(student.getId());
        return student;

    }

    @Override
    public void reset() {
        studentMapper.updateStatusValue0();
    }

    @Override
    public List<Student> querybypage(Integer pagenum, Integer pagesize) {

        // 调用分页插件进行分页操作
        // 参数1：页号
        // 参数2: 页大小
        PageHelper.startPage(pagenum,pagesize);
        List<Student> data =  studentMapper.findAll();


        return data;
    }

    @Override
    public HashMap<String, Object> querybypageInfo(Integer pagenum, Integer pagesize) {
        // 调用分页插件进行分页操作
        // 参数1：页号
        // 参数2: 页大小
        PageHelper.startPage(pagenum,pagesize);
        List<Student> data =  studentMapper.findAll();
        PageInfo<Student> studentPageInfo = new PageInfo<>(data);
        long total = studentPageInfo.getTotal();//总条数
        int pages = studentPageInfo.getPages();//总页数

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("totalcount",total);
        hashMap.put("pagecount",pages);
        hashMap.put("data",data);
        return hashMap;
    }
}
